﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Service.DtoModel.DeskApp;
using Service.DtoModel.DeskApp.pageModel;
using Common;
using SqlSugar;

namespace Service.Implements
{
    public class SysUserService : BaseServer<SysUser>
    {
        public async Task<ApiResult<SysUser>> LoginAsync(SysUser user, bool ismd5 = false)
        {
            var res = new ApiResult<SysUser>() { statusCode = (int)ApiEnum.Error };
            try
            {
                string _uPwd = ismd5 ? user.uPwd : Md5Crypt.Encrypt(user.uPwd);
                string _SQL = " select a.*,b.rName from sys_user a inner join sys_role b on a.rId=b.rId where a.uName=@uName and a.uPwd=@uPwd ";
                var _TB = Db.Ado.GetDataTable(_SQL, new List<SugarParameter>() { new SugarParameter("@uName", user.uName), new SugarParameter("@uPwd", _uPwd) });
                List<SysUser> users = new ModelHandler<SysUser>().FillModel(_TB);
                res.data = users[0];
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 获取当前用户机器的唯一码
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<SysUser>> VerifyIsActive(SysUser parm)
        {
            var res = new ApiResult<SysUser>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var _activeCode = ConfigExtensions.Configuration["CusInfo:ActiveCode"];
                var _res = await Db.Queryable<SysUser>()
                                   .Where(m => m.OnLineMachineInfo.Equals(parm.OnLineMachineInfo) && m.ActiveCode.Equals(_activeCode))
                                   .OrderBy(m => m.AddTime, OrderByType.Desc)
                                   .ToListAsync();
                if (_res.Count == 1)
                {
                    res.data = _res[0];
                    res.statusCode = (int)ApiEnum.Status;
                }
                else
                {
                    res.message = "当前操作不合符";
                    res.statusCode = (int)ApiEnum.Error;
                }
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }
        public async Task<ApiResult<SysUser>> VerifyIsOnly(SysUser parm)
        {
            var res = new ApiResult<SysUser>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var _res = await Db.Queryable<SysUser>()
                                   .Where(m => m.OnLineMachineInfo.Equals(parm.OnLineMachineInfo) && m.ActiveCode.Equals(parm.ActiveCode) && m.uTell.Equals(parm.uTell))
                                   .OrderBy(m => m.AddTime, OrderByType.Desc)
                                   .ToListAsync();
                if (_res.Count == 0)
                {
                    res.statusCode = (int)ApiEnum.Status;
                }
                else
                {
                    res.statusCode = (int)ApiEnum.Error;
                }
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 根据编号获取实体
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<SysUser>> getByGuid(SysUser parm)
        {
            var res = new ApiResult<SysUser>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var _res = await Db.Queryable<SysUser>()
                                   .Where(m => m.uGuid.Equals(parm.uGuid))
                                   .OrderBy(m => m.AddTime, OrderByType.Desc)
                                   .ToListAsync();
                if (_res.Count == 1)
                {
                    res.data = _res[0];
                    res.statusCode = (int)ApiEnum.Status;
                }
                else
                {
                    res.message = "当前操作不合符";
                    res.statusCode = (int)ApiEnum.Error;
                }
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        public async Task<ApiResult<string>> UpById(SysUser parms)
        {
            var res = new ApiResult<string>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var _parms = new List<SugarParameter>() {
                    new SugarParameter("@HeadImg", parms.HeadImg),
                    new SugarParameter("@uName", parms.uName),
                    new SugarParameter("@uPwd", parms.uPwd),
                    new SugarParameter("@uEmail", parms.uEmail),
                    new SugarParameter("@RealName", parms.RealName),
                    new SugarParameter("@Addr", parms.Addr),
                    new SugarParameter("@uGuid", parms.uGuid)
                };
                var sql = " UPDATE sys_user SET HeadImg=@HeadImg,uName=@uName,uPwd=@uPwd,uEmail=@uEmail,RealName=@RealName,Addr=@Addr WHERE uGuid=@uGuid ";
                var _up = Db.Ado.ExecuteCommand(sql, _parms);
                res.statusCode = _up > 0 ? (int)ApiEnum.Status : (int)ApiEnum.Error;
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }
    }
}
